const mixin = require("../../mixins");
Component({
	behaviors: [mixin],
	data: {
		currIndex: 0,
		scrollLeft: 0,
		scrollTop: 0,
		isIDE: !1,
		duration: 0,
		scrollDistance: 0,
		transX: 0
	},
	properties: {
		mf_data: Object
	},
	async attached() {
		const {
			duration: t = 300,
			children
		} = this.properties.mf_data;
		await this.initData()

		this.setData({
			isIDE: "devtools" == wx.getSystemInfoSync().platform,
			duration: t
		})
		this.autoPlay()
	},
	didUpdate(t, e) {},
	detached() {},
	methods: {
		initData() {
		  const { mf_data } = this.properties;
			const page =  getCurrentPages();
			let navName = page[page.length-1].data.navName
		    let obj = mf_data.title && mf_data.title.children.forEach((item, index) => {
				 if(item.name1 == navName) {
					 this.setData({
					 	currIndex: index
					 })
				 }
		  })
		},
		autoPlay() {
			const {
				autoPlay: t = !1,
				autoPlayInterval: e = 3e3
			} = this.properties.mf_data;
			t && (this.timer = setInterval(() => {
				this.handleTouchEnd(-1)
			}, e))
		},
		bindClick(t) {
			const {
				index: e,nav
			} = t.currentTarget.dataset, {
				duration: a = 300,
				title:{children}
			} = this.properties.mf_data;
			this.switch || (this.timerTouch(a), this.setData({
				scrollLeft: this.translateDistance(Number(e)),
				currIndex: Number(e)
			}))
			// this.data.page[this.data.page.length-1].setData({navName:nav.name1 })
			if(children[e]?.trackingParams){
				this.triggerEvent('triggerComponent', {
					data: {
						trackData: children[e]?.trackingParams,
						name1: nav
					}
				}, {
					bubbles: true,
					composed: true
				});
			}
		},
		translateDistance(t = 0) {
			let e, a = this.properties.mf_data.title,
				i = Number(a.children[0].style.width),
				s = i * a.children.length;
			e = i * (t + 1) >= 726 && i * (t + 1) <= s ? t <= a.children.length - 2 ? t - 1 : t - 2 : 0;
			let n = i * e > 0 ? i * e : 0;
			return Number(n)
		},
		touchStart(t) {
			const {
				clientX: e,
				clientY: a
			} = t.changedTouches[0];
			this.startX = e
			this.startY = a

		},
		touchMove() {},
		touchEnd(t) {
			if (Date.now() - this.startTime < this.data.duration) return;
			const {
				clientX: e,
				clientY: a
			} = t.changedTouches[0];
			let  _x = e - this.startX;
			 // Math.abs(a) < 20 || (this.startTime = Date.now(), this.handleTouchEnd(a))
			let y = Math.abs(this.startY - a)
			let x = Math.abs(_x)

			 if(x>(y+10) &&  x>20 && y<200) {
				  (this.startTime = Date.now(), this.handleTouchEnd(_x))
			 }

		},
		timerTouch(t = 200) {
			this.switch = !0, setTimeout(() => {
				this.switch = !1
			}, t)
		},
		handleTouchEnd(t) {
			const {
				animationType: e
			} = this.properties.mf_data;

					this.handleAnimationTypeByOpacity(t);

		},
		handleAnimationTypeByOpacity(t) {
			let {
				currIndex: e
			} = this.data;
			const {
				children: a = []
			} = this.properties.mf_data;
			let i = Number(e);
			t > 0 ? 0 === i ? i = a.length - 1 : i -= 1 : i === a.length - 1 ? i = 0 : i += 1;
			const s = {
				currentTarget: {
					dataset: {
						index: i
					}
				}
			};
			this.bindClick(s)
		},

	}
});
